package com.gxa.customer.service.Impl;

import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.common.pojo.AppUser;
import com.gxa.common.pojo.Country;
import com.gxa.customer.mapper.AppUserMapper;
import com.gxa.customer.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AppUserServiceImpl implements AppUserService {

    @Autowired
    private AppUserMapper userDao;


    @Override
    public JsonResult selectUserByPage(Integer pageNo, Integer pageSize) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(200);
        Integer count=userDao.selectCounts();
        jsonResult.setCount(count);
        Integer begin=(pageNo-1)*pageSize;;
        List<AppUser> appUserList=userDao.selectData(begin,pageSize);
        jsonResult.setData(appUserList);
        return jsonResult;
    }

    @Override
    public AppUser selectUserByPhone(String phone) {
         AppUser user=userDao.selectUserByPhone(phone);
         return user;
    }

    @Override
    public AppUser selectUserByUserNickName(String userNickName) {
        AppUser user=userDao.selectUserByNickName(userNickName);
        return user;
    }

    @Override
    public void insertAppUser(String phone) {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String registerTime=sdf.format(date);
        userDao.insertUser(phone,registerTime);
    }

    @Override
    public JsonResult selectUsersByItems(Integer pageNo, Integer pageSize, String phone, String userNickName) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(200);
        Integer count=userDao.selectCountsByItems(phone,userNickName);
        jsonResult.setCount(count);
        Integer begin=(pageNo-1)*pageSize;;
        List<AppUser> appUserList=userDao.selectDataByItems(begin,pageSize,phone,userNickName);
        jsonResult.setData(appUserList);
        return jsonResult;
    }

    @Override
    public void updateStatus(Long id) {
        userDao.updateStatus(id);
    }

    @Override
    public AppUser selectUserById(Long id) {
        AppUser appUser=userDao.selectUserById(id);
        return appUser;
    }

    @Override
    public void updateUser(AppUser appUser) {
        userDao.updateUser(appUser);
    }

    @Override
    public List<Country> selectAllCountry() {
        List<Country> countries= userDao.selectAllCountry();
        return countries;
    }

    @Override
    public R findUserCollectionMerchant(Long appUserId) {
        R r=new R();
        List<Map<String, Object>> userCollectionMerchant = userDao.findUserCollectionMerchant(appUserId);
        if (userCollectionMerchant!=null){
            r.setCode(200);
            r.setMsg("查询用户收藏的商海信息成功！");
            r.setData(userCollectionMerchant);
        }else{
            r.setCode(500);
            r.setMsg("查询用户收藏的商海信息失败！");
        }
        return r;
    }

    @Override
    public int registerByPhone(String phone) {
        return userDao.registerByPhone(phone);
    }

    @Override
    public Map<String, Object> findUserInfoByPhone(String phone) {
        return userDao.findUserInfoByPhone(phone);
    }


}
